<?php
namespace Nawa;
/**
 * Survey Header.
 * 
 * It contain the Id of the Survey, the category, duration
 * ...
 * 
 * @property int	$surveyid		Id
 * @property string	$language		Language
 * @property string	$label			Survey Label
 * @property string	$category		Category
 * @property int	$type			Type
 * @property int	$duration		Expected duration time in sec
 * @property text	$description	Description Message
 * @property text	$introduction	Introduction Message
 * @property text	$conclusion		Conclusion Message
 * 
 * @method array sections()
 */
class Survey extends \Fox\Object\Versioned {
	
	/**
	 * Link to Sections
	 * @var string
	 */
	const LINK_SECTIONS = 'sections';

	/**
	 * Link to Translation
	 * @var string
	 */
	const LINK_I18N = 'i18n';
	
	/**
	 * Link to the survey questions object
	 * @var string
	 */
	const LINK_QUESTIONS = 'questions';
		
	/**
	 * Change when add a new section
	 * @var string
	 */
	const CHANGE_ADD_SECTION = 'section-add';
	
	/**
	 * Change when update a section
	 * @var string
	 */
	const CHANGE_UPDATE_SECTION = 'section-update';
	
	/**
	 * Change when delete a section
	 * @var string
	 */
	const CHANGE_DELETE_SECTION = 'section-delete';		
	
	/**
	 * Process i18n to get the translated value if it exist,
	 * else the system will default to page value
	 * @param string $fieldName
	 * @return string
	 */
	public function i18nParse($fieldName) {
		$language = \Xview::instance()->language();
		$defaultLanguage = $this->language;
		if($language != $defaultLanguage) {
			$i18n = $this->findI18n($this->getInlineId().':'.$language);
			if($i18n->exists()) {
				return $i18n->$fieldName;
			}
			elseif(!empty($defaultLanguage)) {
				return $this->$fieldName;
			}
		}
		return $this->$fieldName;
	}
	
	/**
	 * Return Title
	 * @return string Element Title
	 */
	public function label() {
		return $this->i18nParse('label');
	}
	
	/**
	 * Build the Survey
	 * This method is going to load all sections and all questions
	 * associate to each section 
	 * @return \Fox\Node
	 */
	public function tree() {
		$root = new \Fox\Node(\Fox\Node::ROOT_NODE);
		foreach($this->sections() as $section) {
			$sectionNode = Survey\Section::tree($section);
			$root->add($sectionNode);
		}
		return $root;
	}
	
	/**
	 * 
	 * Render as HTML the full survey
	 * @return string
	 */
	public function render($initialize = null) {
		$index = 0;
		$html = '<div class="survey" data-id="'.$this->surveyid.'">';
		foreach($this->sections() as $section) {
			$index++;
			$html .= '<ul class="survey-section"><h3>'.$index.'.0 '.$section->label().'</h3>';
			$html .= '<div class="survey-introduction">'.$section->introduction().'</div>';
			$sectionTree = Survey\Section::tree($section);
			if(!empty($initialize))  {
				$sectionTree->initialize('id',$initialize,true);
			}
			$qIndex= 0;
			foreach($sectionTree->getChildren() as $questionNode) {
				$qIndex++;
				$html .= Survey\Question::renderTree($questionNode,$index.'.'.$qIndex);
			}
			$html .= '<div class="survey-conclusion">'.$section->conclusion().'</div>';
			$html .= '</ul>';
		}
		$html .= '</div>';
		return $html;
	}
	
	/**
	 * 
	 * Render As PDF
	 * @param \Fox\Renderer\Pdf $pdf
	 * @param array $initialize
	 * @param int $index
	 */
	public function renderAsPDF(\Fox\Renderer\Pdf &$pdf,$initialize,$index = 0) {
		foreach($this->sections() as $section) {
			$index++;
			// Right Margin
			$pdf->Cell(5);
			$pdf->SetFont('',$pdf::FONT_BOLD,$pdf::FONT_SIZE_H3);
			$pdf->Cell(0,8,$index.'.0 '.$section->label(),0,0,'L');
			$pdf->Ln();
			// Display Section Introduction
			$introduction = $section->introduction();
			if(!empty($introduction)) {
				$pdf->SetFont('','I',$pdf::FONT_SIZE_NORMAL);
				$pdf->Cell(5);
				$pdf->Cell(0,8,$introduction,0,0,'L');
				$pdf->Ln();
			}

			$sectionTree = Survey\Section::tree($section);
			if(!empty($initialize))  {
				$sectionTree->initialize('id',$initialize,true);
			}
			$qIndex= 0;
			foreach($sectionTree->getChildren() as $questionNode) {
				$qIndex++;
				Survey\Question::renderTreeAsPDF($pdf,$questionNode,$index.'.'.$qIndex);
			}
			$conclusion = $section->conclusion();
			if(!empty($introduction)) {
				$pdf->SetFont('','I',$pdf::FONT_SIZE_NORMAL);
				$pdf->Cell(5);
				$pdf->Cell(0,8,$conclusion,0,0,'L');
				$pdf->Ln();
			}
		}
		return $pdf;
	}
}

\Fox\Dao::infos(Survey::className(),new \Fox\Object\Info('surveys', \Fox\Databases::DB_APP));

// Link To Sections
\Fox\Object\Links::add(\Fox\Dao::link(Survey\Section::className(),Survey\Section::LINK_SURVEY)
	->to(Survey::className(), Survey::LINK_SECTIONS)
	->on('surveyid')
	->orderBy('position')
	->versionFrom()
);

// Link To Questions
\Fox\Object\Links::add(\Fox\Dao::link(Survey\Question::className(),Survey\Question::LINK_SURVEY)
	->to(Survey::className(), Survey::LINK_QUESTIONS)
	->on('surveyid')
);

// Link to i18n elements
\Fox\Object\Links::add(\Fox\Dao::link(Survey\I18n::className(),Survey\I18n::LINK_SURVEY)
->to(Survey::className(), Survey::LINK_I18N)
->on('surveyid')
->versionFrom());